package features.advance.jdk.io.blocking;

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;

import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LIN
 * @date 2021-06-08
 */
public class Client {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                Socket socket = new Socket("127.0.0.1",8081);

                OutputStream outputStream = socket.getOutputStream();
                System.out.println(Thread.currentThread().getName()+"客户端连接成功");
                Thread.sleep(3000);
                Map<String,Object> map = new HashMap<>(16);
                map.put("num",50);
                map.put("message",(Thread.currentThread().getName()+":hello world"));
                outputStream.write(JSONObject.toJSONBytes(map));
                outputStream.flush();
                outputStream.close();
                socket.close();

            }
        };
        for (int i = 0; i < 1; i++) {
            new Thread(runnable).start();
        }
    }
}
